/*
helferlein.com ( http://www.helferlein.com )
Michael Tobisch

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions 
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using System.Data;
using DotNetNuke.Common.Utilities;
using helferlein.DNN.Modules.BabelFish.Business;
using System.Globalization;

namespace helferlein.DNN.Modules.Form.Business
{
   public class FormItemInfo : IHydratable
   {
#region Private Properties
      private int _portalID;
      private string _locale;
      private BabelFishController babelFishController = null;
      protected BabelFishController BabelFishController
      {
         get
         {
            if (babelFishController == null)
               babelFishController = new BabelFishController();
            return babelFishController;
         }
      }
#endregion

#region Public Properties
      public int PortalID { get { return _portalID; } }
      public int ModuleID { get; set; }
      public int FormItemID { get; set; }
      public FormItemType FormItemType { get; set; }
      public int? FormItemRepeatColumns { get; set; }
      public int? SortValue { get; set; }
      public bool FormItemRequired { get; set; }
      public bool FormItemOther { get; set; }
      public string FormItemOtherValue { get; set; }
      public int? Width{ get; set; }
      public int? Height { get; set; }
      public LinkTarget? FormItemLinkTarget { get; set; }
      public string ValidationExpression { get; set; }
      public string Locale
      {
         get
         {
            if (String.IsNullOrEmpty(_locale))
               _locale = CultureInfo.CurrentCulture.Name;
            return _locale;
         }
         set { _locale = value; }
      }

      public string FormItemValue { get; set; }
      public string FormItemSelectedValue { get; set; }
      public string FormItemOriginalFileName { get; set; }
      public string FormItemLabel { get; set; }
      public string FormItemHelpText { get; set; }
      public string FormItemTemplate { get; set; }
#endregion

#region Constructors
      public FormItemInfo()
      {
         this.FormItemTemplate = String.Empty;
      }

      public FormItemInfo(int portalID, int moduleID, int formItemID, FormItemType formItemType, int? formItemRepeatColumns, int sortValue, bool formItemRequired, bool formItemOther, int? width, int? height, LinkTarget? formItemLinkTarget, string validationExpression, string locale, string formItemValue, string formItemSelectedValue, string formItemLabel, string formItemHelpText, string formItemTemplate)
      {
         this.ModuleID = moduleID;
         this.FormItemID = formItemID;
         this.FormItemType = formItemType;
         this.FormItemRepeatColumns = formItemRepeatColumns;
         this.SortValue = sortValue;
         this.FormItemRequired = formItemRequired;
         this.FormItemOther = formItemOther;
         this.Width = width;
         this.Height = height;
         this.FormItemLinkTarget = formItemLinkTarget;
         this.ValidationExpression = validationExpression;
         this.Locale = locale;
         this.FormItemValue = formItemValue;
         this.FormItemSelectedValue = formItemSelectedValue;
         this.FormItemLabel = formItemLabel;
         this.FormItemHelpText = formItemHelpText;
         this.FormItemTemplate = formItemTemplate;
      }
#endregion

#region IHydratable Members

      public void Fill(IDataReader dr)
      {
         this._portalID = Convert.ToInt32(Null.SetNull(dr["PortalID"], this.PortalID));
         this.ModuleID = Convert.ToInt32(Null.SetNull(dr["ModuleID"], this.ModuleID));
         this.FormItemID = Convert.ToInt32(Null.SetNull(dr["FormItemID"], this.FormItemID));
         this.FormItemType = (FormItemType)Convert.ToInt32(Null.SetNull(dr["FormItemType"], this.FormItemType));
         this.FormItemRepeatColumns = (int?)Null.SetNull(dr["FormItemRepeatColumns"], this.FormItemRepeatColumns);
         this.SortValue = (int?)Null.SetNull(dr["SortValue"], this.SortValue);
         this.FormItemRequired = Convert.ToBoolean(Null.SetNull(dr["FormItemRequired"], this.FormItemRequired));
         this.FormItemOther = Convert.ToBoolean(Null.SetNull(dr["FormItemOther"], this.FormItemOther));
         this.Width = (int?)Null.SetNull(dr["Width"], this.Width);
         this.Height = (int?)Null.SetNull(dr["Height"], this.Height);
         this.FormItemLinkTarget = (LinkTarget?)Convert.ToInt32(Null.SetNull(dr["FormItemLinkTarget"], this.FormItemLinkTarget));
         this.ValidationExpression = Convert.ToString(Null.SetNull(dr["ValidationExpression"], this.ValidationExpression));

         BabelFishInfo fish = this.BabelFishController.GetString(this.PortalID, this.Locale, "helferlein.Form.FormItem." + this.FormItemID.ToString(), "FormItemValue");
         this.FormItemValue = (fish == null ? String.Empty : fish.DisplayValue);

         fish = this.BabelFishController.GetString(this.PortalID, this.Locale, "helferlein.Form.FormItem." + this.FormItemID.ToString(), "FormItemSelectedValue");
         this.FormItemSelectedValue = (fish == null ? String.Empty : fish.DisplayValue);

         fish = this.BabelFishController.GetString(this.PortalID, this.Locale, "helferlein.Form.FormItem." + this.FormItemID.ToString(), "FormItemLabel");
         this.FormItemLabel = (fish == null ? String.Empty : fish.DisplayValue);

         fish = this.BabelFishController.GetString(this.PortalID, this.Locale, "helferlein.Form.FormItem." + this.FormItemID.ToString(), "FormItemHelpText");
         this.FormItemHelpText = (fish == null ? String.Empty : fish.DisplayValue);

         fish = this.BabelFishController.GetString(this.PortalID, this.Locale, "helferlein.Form.FormItem." + this.FormItemID.ToString(), "FormItemTemplate");
         this.FormItemTemplate = (fish == null ? String.Empty : fish.DisplayValue);
      }

      public int KeyID
      {
         get { return this.FormItemID; }
         set { this.FormItemID = value; }
      }
#endregion
   }

   public class FormItemComparer
   {
      public int Compare(FormItemInfo x, FormItemInfo y)
      {
         int result;
         if (x.SortValue == null)
            result = ((y.SortValue == null) ? 0 : -1);
         else
         {
            if (y.SortValue == null)
               result = 1;
            else
            {
               if (x.SortValue == y.SortValue)
                  result = 0;
               else if (x.SortValue < y.SortValue)
                  result = -1;
               else
                  result = 1;
            }
         }
         return result;
      }
   }
}